<?php

require_once ('system/libraries/Model.php');
require_once ('system/application/models/NewsModel.php');
class NewsUtils extends Model {
	
	private static $instance = null;
	
	protected function __construct() {
		parent::__construct ();
	}
	
	public static function getInstance() {
		if (NewsUtils::$instance == null) {
			NewsUtils::$instance = new NewsUtils();
		}
		
		return NewsUtils::$instance;
	}
	/**
	 * Static method that collects the latest news for the page $page
	 * for $newsPerPage news per page for language $langID
	 *
	 * @param unknown_type $page
	 * @param unknown_type $newsPerPage
	 * @param unknown_type $langID
	 * @return array of NewsModel
	 */
	public function getNews($page, $newsPerPage, $langID) {
		
		$start = $page * $newsPerPage;
		$queryString = "select n.*, m.content as title, m1.content as text 
								from news n,messages m, messages m1 
								where n.langID=$langID and n.titleMsgID=m.messageID and 
										n.textMsgID=m1.messageID 
								limit $start, $newsPerPage";
		$query = $this->db->query($queryString);
		
		$allNews = array();
		foreach ($query->result() as $row) {
			$tempNews = new NewsModel();
			$tempNews->setNewsID($row->newsID);
			$tempNews->setTitle($row->title);
			$tempNews->setText($row->text);
			$tempNews->setTextMsgID($row->textMsgID);
			$tempNews->setTitleMsgID($row->titleMsgID);
			$tempNews->setUserID($row->userID);
			$tempNews->setLangID($row->langID);
			$tempNews->setDate($row->date);
			$allNews[] = $tempNews;
		}
		return $allNews;
	}
}

?>
